package com.kasax.plugin.app.visitor.lifecycle3

import com.kasax.plugin.app.LogUtils
import org.objectweb.asm.*

class NewLifecycleClassVisitor extends ClassVisitor {
    static String TAG = "NewLifecycleClassVisitor"
    private String className
    private boolean isActivity = false
    private boolean isFragment = false

    NewLifecycleClassVisitor(ClassVisitor classVisitor) {
        super(Opcodes.ASM7, classVisitor)
    }

    public static byte[] processClass(byte[] classBytes) {
//        LogUtils.d(TAG, "processClass")
        def cr = new ClassReader(classBytes)
        def cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS)
        def cv = new NewLifecycleClassVisitor(cw)
        cr.accept(cv, ClassReader.EXPAND_FRAMES)
        return cw.toByteArray()
    }

    @Override
    void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        super.visit(version, access, name, signature, superName, interfaces)
        this.className = name
        // 检测是否为Activity或Fragment子类
        isActivity = isSupportedActivity(superName)
        isFragment = isSupportedFragment(superName)
        LogUtils.d(TAG, "visit clsName: $name , super-name: $superName , isActivity: $isActivity , isFragment: $isFragment")

    }

    boolean isSupportedActivity(String targetClsName) {
        return null != targetClsName && (targetClsName.equals("androidx/appcompat/app/AppCompatActivity") ||
                targetClsName.equals("android/app/Activity") ||
                targetClsName.startsWith("android/support/v4/app/"))
    }

    boolean isSupportedFragment(String targetClsName) {
        return null != targetClsName && (targetClsName.equals("androidx/fragment/app/Fragment") ||
                targetClsName.equals("android/app/Fragment") ||
                targetClsName.startsWith("android/support/v4/app/Fragment"))
    }

    private boolean lifecycleMethods(String name, String descriptor) {
        String[] lifecycleMethods = [
                "onCreate", "(Landroid/os/Bundle;)V",
                "onStart", "()V",
                "onResume", "()V",
                "onPause", "()V",
                "onStop", "()V",
                "onDestroy", "()V",
                "onCreateView", "(Landroid/view/LayoutInflater;Landroid/view/ViewGroup;Landroid/os/Bundle;)Landroid/view/View;"
        ]

        for (int i = 0; i < lifecycleMethods.length; i += 2) {
            if (name.equals(lifecycleMethods[i]) && descriptor.equals(lifecycleMethods[i + 1])) {
                return true;
            }
        }
        return false;
    }

    @Override
    MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions)
        LogUtils.d(TAG, "visitMethod clsName: $className , method-name: $name")
        if (mv == null) {
            return null
        }

        // 处理Activity/Fragment生命周期方法
        if (isActivity || isFragment) {
            if (lifecycleMethods(name, descriptor)) {
                LogUtils.d(TAG, "visitMethod init NewLifecycleMethodVisitor")
                return new NewLifecycleMethodVisitor(mv, access, name, descriptor, className)
            }
        }
//        // 处理onClick方法（防抖）
//        if (name.equals("onClick") && descriptor.equals("(Landroid/view/View;)V")) {
//            return new ClickDebounceMethodVisitor(mv, access, name, descriptor, className)
//        }
        return mv
    }
}


